package com.zycfc.zsf.boot.util.ead;

import java.nio.*;
import java.math.*;

public class Murmurs
{
    public static Long hash(final byte[] key) {
        final ByteBuffer buf = ByteBuffer.wrap(key);
        final int seed = 305441741;
        final ByteOrder byteOrder = buf.order();
        buf.order(ByteOrder.LITTLE_ENDIAN);
        final long m = -4132994306676758123L;
        final int r = 47;
        long h = (long)seed ^ buf.remaining() * m;
        while (buf.remaining() >= 8) {
            long k = buf.getLong();
            k *= m;
            k ^= k >>> r;
            k *= m;
            h ^= k;
            h *= m;
        }
        if (buf.remaining() > 0) {
            final ByteBuffer finish = ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN);
            finish.put(buf).rewind();
            h ^= finish.getLong();
            h *= m;
        }
        h ^= h >>> r;
        h *= m;
        h ^= h >>> r;
        buf.order(byteOrder);
        return h;
    }
    
    public static Long hash(final String key) {
        return hash(key.getBytes());
    }
    
    public static BigDecimal readUnsignedLong(final long value) {
        if (value >= 0L) {
            return new BigDecimal(value);
        }
        final long lowValue = value & Long.MAX_VALUE;
        return BigDecimal.valueOf(lowValue).add(BigDecimal.valueOf(Long.MAX_VALUE)).add(BigDecimal.valueOf(1L));
    }
    
    public static BigDecimal hashUnsigned(final String key) {
        return readUnsignedLong(hash(key));
    }
    
    public static BigDecimal hashUnsigned(final byte[] key) {
        return readUnsignedLong(hash(key));
    }
    
    public static void main(final String[] args) {
        for (int i = 0; i < 100; ++i) {
            System.out.println(hashUnsigned("CUS2041272819880107603X").toPlainString());
        }
    }
}
